<?php
class id5_mcrypt
{
	function id5_mcrypt()
	{
		if(!function_exists('mcrypt_module_open'))
		{
			exit('Fatal error: no mcrypt module ');
		}
		
	}
	function set_key($key)
	{
		$this->mcrypt_key = $key;
	}
	function encrypt($value,$key)
	{
		$td = mcrypt_module_open('tripledes', '', 'ecb', '');
		$td_size = mcrypt_enc_get_iv_size($td);
		$iv = mcrypt_create_iv($td_size,MCRYPT_RAND);
		$key = substr($key, 0, $td_size);
		mcrypt_generic_init($td, $key, $iv);
		$ret = base64_encode(mcrypt_generic($td, $value));
		mcrypt_generic_deinit($td);
		mcrypt_module_close($td);
		return $ret;
	}
	function decrypt($value,$key)
	{
		$td = mcrypt_module_open('tripledes', '', 'ecb', '');
		$td_size = mcrypt_enc_get_iv_size($td);
		$iv = mcrypt_create_iv($td_size,MCRYPT_RAND);
		$key = substr($key, 0, $td_size);
		mcrypt_generic_init($td, $key, $iv);
		$value = base64_decode($value);
		$ret = trim(mdecrypt_generic($td, $value));
		mcrypt_generic_deinit($td);
		mcrypt_module_close($td);
		return $ret;
	}
	function id5_encrypt($icpcode,$uid)
	{
		$temp = "{$icpcode}||{$uid}||".time();
		return $this->encrypt($temp,$this->mcrypt_key);
	}
	function id5_decrypt($encrypt_value)
	{
		$temp = $this->decrypt($encrypt_value,$this->mcrypt_key);
		$temp_arr = preg_split("/\|\|/",$temp);
		return array("icpcode"=>$temp_arr[0],"un_id" =>$temp_arr[1],"create_code_time"=>$temp_arr[2]);
	}
}
/*
$id5 = new id5_mcrypt();
$key = "sanshisanshisanshisanshisanshi";
$id5->set_key($key);
$temp = $id5->id5_encrypt("10000","sanshi");
print_r($id5->id5_decrypt($temp));
#$temp = $id5->encrypt("sanshisanshisanshisanshisanshisanshisanshisanshisanshisanshi",$key);
#echo $temp."--". $id5->decrypt($temp,$key);
*/
?>